package dshop.action.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.RecordKit;
import appbase.utils.DateUtil;
import appbase.utils.TimeRange;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.draw.DrawDetail;

/**
 * 首页管理
 */
@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class HomeAction extends ApiBaseController{
	
	public void sta()
	{
		TimeRange today = DateUtil.getTimeRangeOfToday();
		TimeRange week = DateUtil.getOfCurrentWeek();
		TimeRange month = DateUtil.getOfCurrentMonth();
		
		memberCnt(today, week, month);
		vipPersonCnt(today, week, month);
		vipCompanyCnt(today, week, month);
		vipUnionCnt(today, week, month);
		procurementCnt(today, week, month);
		jobCnt(today, week, month);
		
		renderSuccess();
	}
	
	public void integral()
	{
		String userphone = getPara("userphone", "").trim();
		
		String select = "select sd.money,sd.descs,sd.create_time,event_param,m.phone,CONCAT(m.last_name,m.first_name) 'invite'";
		String where = "from sp_commission_detail sd left join sp_commission_event se on(sd.change_ref_id = se.id) left join member_general_info m on(se.user_id = m.account_id)"
				+" where sd.user_id = (select id from account where phone = ?)";
					 	
		String orderBy = " order by sd.create_time desc ";
		
		List<Object> params = new ArrayList<Object>();
		params.add(userphone);
		
		Page<Record> page = page(select, where + orderBy, params.toArray());
		
		renderSuccess();
	}

	private void memberCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = memberSta(null);
		int dayCnt = memberSta(today);
		int weekCnt = memberSta(week);
		int monthCnt = memberSta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("member", r);
	}
	
	private void procurementCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = procurementSta(null);
		int dayCnt = procurementSta(today);
		int weekCnt = procurementSta(week);
		int monthCnt = procurementSta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("procurement", r);
	}
	
	private void jobCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = jobSta(null);
		int dayCnt = jobSta(today);
		int weekCnt = jobSta(week);
		int monthCnt = jobSta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("job", r);
	}
	
	private void vipPersonCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = vipSta(null);
		int dayCnt = vipSta(today);
		int weekCnt = vipSta(week);
		int monthCnt = vipSta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("vip", r);
	}
	
	private void vipCompanyCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = vipCompanySta(null);
		int dayCnt = vipCompanySta(today);
		int weekCnt = vipCompanySta(week);
		int monthCnt = vipCompanySta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("vipCompany", r);
	}
	private void vipUnionCnt(TimeRange today, TimeRange week, TimeRange month) 
	{
		int cnt = vipUnionSta(null);
		int dayCnt = vipUnionSta(today);
		int weekCnt = vipUnionSta(week);
		int monthCnt = vipUnionSta(month);
		
		Record r = RecordKit.createRecord("cnt, dayCnt, weekCnt, monthCnt", cnt, dayCnt, weekCnt, monthCnt);
		setAttr("vipUnion", r);
	}
	
	private int memberSta(TimeRange timeRange) 
	{
		String sql = " select count(id) as cnt from account ";
		List<Object> params = new ArrayList<Object>();
		if (timeRange != null)
		{
			sql += " where register_date_time >= ? and register_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
	
	private int procurementSta(TimeRange timeRange) 
	{
		String sql = " select count(id) as cnt from bbs_procurement where is_delete = 'NO' ";
		List<Object> params = new ArrayList<Object>();
		if (timeRange != null)
		{
			sql += " and add_date_time >= ? and add_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
	
	private int jobSta(TimeRange timeRange) 
	{
		String sql = " select count(id) as cnt from bbs_job where is_delete = 'NO' ";
		List<Object> params = new ArrayList<Object>();
		if (timeRange != null)
		{
			sql += " and add_date_time >= ? and add_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
	
	private int vipSta(TimeRange timeRange) 
	{
		Date now = new Date();
		
		String sql = " select count(1) as cnt from member_vip_detail where level_1_start_date_time <= ? and level_1_end_date_time >= ? ";
		List<Object> params = new ArrayList<Object>();
		params.add(now);
		params.add(now);
		if (timeRange != null)
		{
			sql += " and level_1_start_date_time >= ? and level_1_start_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
	private int vipCompanySta(TimeRange timeRange) 
	{
		Date now = new Date();
		
		String sql = " select count(1) as cnt from member_vip_detail where level_2_start_date_time <= ? and level_2_end_date_time >= ? ";
		List<Object> params = new ArrayList<Object>();
		params.add(now);
		params.add(now);
		if (timeRange != null)
		{
			sql += " and level_2_start_date_time >= ? and level_2_start_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
	private int vipUnionSta(TimeRange timeRange) 
	{
		Date now = new Date();
		
		String sql = " select count(1) as cnt from member_vip_detail where (level_3_start_date_time <= ? and level_3_end_date_time >= ?";
		List<Object> params = new ArrayList<Object>();
		params.add(now);
		params.add(now);
		if (timeRange != null)
		{
			sql += " and level_3_start_date_time >= ? and level_3_start_date_time <= ? ";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		sql+= " ) or (level_4_start_date_time <= ? and level_4_end_date_time >= ? ";
		params.add(now);
		params.add(now);
		if (timeRange != null)
		{
			sql += " and level_3_start_date_time >= ? and level_3_start_date_time <= ?";
			params.add(timeRange.getStartTime());
			params.add(timeRange.getEndTime());
		}
		sql+= " )";
		
		Record r = Db.findFirst(sql, params.toArray());
		if (r == null || r.get("cnt") == null)
		{
			return 0;
		}
		return r.getLong("cnt").intValue();
	}
}
